﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Db4objects.Db4o.Config;
using Db4objects.Db4o.Constraints;
using DonNicky.Db.Db4o.Config.Attributes;

namespace DonNicky.Db.Db4o
{
    public static class IEmbeddedConfigurationExtensions
    {
        public static void UseExtensions(this IEmbeddedConfiguration cfg, Assembly assembly)
        {
            // find fields marked Unique
            var res = from t in assembly.GetTypes()
                      //where t.BaseType == typeof(Entity)
                      from f in t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
                      where f.IsDefined(typeof(UniqueAttribute), true)
                      select f;
            // add unique fields in db4o configuration
            foreach (var item in res)
                cfg.Common.Add(new UniqueFieldValueConstraint(item.DeclaringType, item.Name));
        }
    }
}
